###################################################################################################
## Payson (2020) "The Partisan Logic of City Mobilization" Replication Code
## On-Line Appendix
## Created with R version 3.4.1 (2017-06-30) -- "Single Candle"
###################################################################################################


## Set working directory
setwd()


## Load packages
library(plm); library(xtable); library(maps); library(ggplot2); library(RColorBrewer); 
library(glmnet); library(plyr); library(reshape); library(leaps); library(dummies); 
library(effects); library(lmtest); library(sandwich); library(stargazer)
library(lfe); library(DataCombine); library(dplyr)


## Functions for clean tex tables (Note: tables will not compile without these functions)
pretty <- function(x) {
  paste(prettyNum(x, big.mark = ","))}

num.cities <- function(x) {
  length(which(getfe(x)=="fips"))}

N <- function(x) {
  x$N}

getmean <- function(x) {
  round(mean(x$response), 2)}


dta <- read.csv("panel.csv")



###################################################################################################
## Table A2: City Lobbying Rates and Expenditures by State
###################################################################################################


ddply(dta, .(state), summarize,
  n = length(state) / 9,
  lobby =  length(which(ever.lobby == "Lobby")) / 9,
  min.amount = min(lobby.amount, na.rm = T),
  median.amount = median(lobby.amount, na.rm = T),
  mean.amount = round(mean(lobby.amount, na.rm = T)),
  max.amount = max(lobby.amount, na.rm = T))


###################################################################################################
## Table A3: City Lobbying Rates and Expenditures by State
###################################################################################################

summary <- data.frame(ever.lobby = dta$ever.lobby, state.lobby = dta$lobby, 
                      mean.num.reps = dta$num.reps, pop = dta$imputed.pop, 
                      income = dta$median.income, log.own.source = dta$log.own.source, 
                      pct.white = dta$pct.white, log.med.house = dta$median.house.value, 
                      pres.vote = dta$pres.dem.2008)


## All Cities
stargazer(summary, summary.stat = c("n", "mean", "sd", "min", "max"), digits = 2,
                 type = "text")

## Never Lobby
stargazer(subset(summary, ever.lobby == "Never"), 
          summary.stat = c("n", "mean", "sd", "min", "max"), digits = 2,
                  type = "text")

## Sometimes Lobby
stargazer(subset(summary, ever.lobby == "Lobby"), 
                  summary.stat = c("n", "mean", "sd", "min", "max"), digits = 2, type = "text")



###################################################################################################
## Table A4: Correlates of City Lobbying State Government, 2006 - 2014
###################################################################################################

m <- felm(lobby ~ log.pop + log.income + log.income.sq + log.own.source + 
            pct.white + log.med.house | state + year, data = dta)

m1 <- felm(lobby ~ log.pop + log.income + log.income.sq+ log.own.source + 
             pct.white + log.med.house | state.by.year, data = dta)


stargazer(m, m1, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          title = c("\\textbf{Correlates of City Lobbying State Government, 2006-2014}."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Population (Log)", "Median Income (Log)", 
                               "Median Income Squared (Log)", "Own Source Revenue (Log)", 
                               "\\% White", "Median House Value (Log)"),
          add.lines = list(c("Mean Lobbying Probability", getmean(m), getmean(m1)), 
                           c("State FEs", "Y", ""),
                           c("Year FEs", "Y", ""),
                           c("State-Year FEs", "", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", "1,244", "1,244")),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
## Robustness Checks: Difference-in-Differences Design
###################################################################################################

###################################################################################################
## Table A5: Effect of Partisan Mismatch on City Lobbying: Different Mis- match Thresholds
###################################################################################################

dta$mismatch.50 <- ifelse((dta$pres.dem.median == "Democrat" & dta$prop.D < .5) |
                         (dta$pres.dem.median == "Republican" & dta$prop.D > .5), 1, 0)

dta$mismatch.60 <- ifelse((dta$pres.dem.median == "Democrat" & dta$prop.D < .4) |
                          (dta$pres.dem.median == "Republican" & dta$prop.D > .6), 1, 0)


dta$mismatch.80 <- ifelse((dta$pres.dem.median == "Democrat" & dta$prop.D < .2) |
                          (dta$pres.dem.median == "Republican" & dta$prop.D > .8), 1, 0)

dta$mismatch.100 <- ifelse((dta$pres.dem.median == "Democrat" & dta$prop.D == 0) |
                          (dta$pres.dem.median== "Republican" & dta$prop.D == 1), 1, 0)



m <- felm(lobby ~ mismatch.50 + log.pop + log.income + log.own.source +
            pct.white + log.med.house | fips + state.by.year | 0 | fips, 
          data = dta)

m1 <- felm(lobby ~ mismatch.60 + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = dta)

m2 <- felm(lobby ~ mismatch.80 + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = dta)

m3 <- felm(lobby ~ mismatch.100 + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = dta)


## Table: Effect of Partisan Mismatch on City Lobbying: Different Thresholds
stargazer(m, m1, m2, m3, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          keep = c("mismatch", "mismatch1", "mismatch2", "mismatch3", "log.pop"),
          title = c("Effect of Partisan Mismatch on City Lobbying: Different Mismatch
                    Thresholds."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Partisan Mismatch (50%)", "Partisan Mismatch (60%)",
                               "Partisan Mismatch (80%)", "Partisan Mismatch (100%)"),
          add.lines = list(c("City FEs", "Y", "Y", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y", "Y", "Y"),
                           c("Full Controls", "Y", "Y", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2)), pretty(num.cities(m3))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), 
                             getmean(m2), getmean(m3))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



#################################################################################################
## Table A6: Specify mismatch based on median city voteshare
###################################################################################################

m <- felm(lobby ~ median.mismatch + log.pop | fips + year | 0 | fips + state.by.year, 
          data = dta)

m1 <- felm(lobby ~ median.mismatch + log.pop | fips + state.by.year | 0 | fips + state.by.year, 
           data = dta)

m2 <- felm(lobby ~ median.mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
           data = dta)


## Table: Effect of Partisan Mismatch on City Lobbying: City Median Specification
stargazer(m, m1, m2, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          keep = c("mismatch", "log.pop"),
          title = c("Effect of Partisan Mismatch on City Lobbying: City Median Specification."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Partisan Mismatch", "Population (Log)"),
          add.lines = list(c("City FEs", "Y", "Y", "Y"),
                           c("Year FEs", "Y", ""),
                           c("State-Year FEs", "", "Y", "Y"),
                           c("Full Controls", "", "", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
## Table A7: Effect of Partisan Misamtch on City Lobbying Expenditures
###################################################################################################

m <- felm(log.lobby.amount ~ mismatch | fips + state.by.year | 0 | fips + state.by.year, 
          data = subset(dta, pres.dem.tercile != "Moderate" & lobby.amount > 0))
summary(m)

m1 <- felm(log.lobby.amount ~ mismatch + log.pop | fips + state.by.year | 0 | fips + state.by.year, 
           data = subset(dta, pres.dem.tercile != "Moderate" & lobby.amount > 0))
summary(m1)

m2 <- felm(log.lobby.amount ~ mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
           data = subset(dta, pres.dem.tercile != "Moderate" & lobby.amount > 0))
summary(m2)


stargazer(m, m1, m2, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          keep = c("mismatch", "log.pop"),
          title = c("Effect of Partisan Mismatch on City Lobbying Expenditures."),
          dep.var.caption = "Lobbying Expenditures (Log)", dep.var.labels.include = FALSE,
          covariate.labels = c("Partisan Mismatch", "Population (Log)"),
          add.lines = list(c("City FEs", "Y", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y", "Y"),
                           c("Full Controls", "", "", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2))),
                           c("Mean Expenditures (Log)", getmean(m), getmean(m1), getmean(m2))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
# Table A8: Effect of Partisan Mismatch on Republican and Democratic Cities
###################################################################################################

m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
            pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
          data = subset(dta, pres.dem.tercile != "Moderate"))

m1 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
           data = subset(dta, pres.dem.tercile == "Democrat"))

m2 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
           data = subset(dta, pres.dem.tercile == "Republican"))


## Table: Effect of Partisan Mismatch on City Lobbying
stargazer(m, m1, m2, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          keep = c("mismatch", "log.pop"),
          title = c("Effect of Partisan Mismatch on Republican and Democratic Cities."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Partisan Mismatch", "Population (Log)"),
          add.lines = list(c("City FEs", "Y", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y", "Y"),
                           c("Full Controls", "Y", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
## Table A9: Effect of Partisan Mismatch on City Lobbying (Excluding Largest Cities)
###################################################################################################

m <- felm(lobby ~ mismatch + log.pop | fips + year | 0 | fips, 
          data = subset(dta, pres.dem.tercile != "Moderate" & imputed.pop < 100000))

m1 <- felm(lobby ~ mismatch + log.pop | fips + state.by.year | 0 | fips, 
          data = subset(dta, pres.dem.tercile != "Moderate" & imputed.pop < 100000))

m2 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = subset(dta, pres.dem.tercile != "Moderate" & imputed.pop < 100000))

stargazer(m, m1, m2, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          keep = c("mismatch", "log.pop"),
          title = c("Effect of Partisan Mismatch on City Lobbying (Excluding Largest Cities)"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Partisan Mismatch", "Population (Log)"),
          add.lines = list(c("City FEs", "Y", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y", "Y"),
                           c("Full Controls", "Y", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)),
                             pretty(num.cities(m2))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
## Table A9: Effect of Partisan Mismatch on City Lobbying (Excluding California, Washington)
###################################################################################################

dta$big.state <- ifelse(dta$state == "California" | dta$state == "Texas" |
                          dta$state == "Washington", 1, 0)


m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
            pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
          data = subset(dta, pres.dem.tercile != "Moderate"))

m1 <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips + state.by.year, 
           data = subset(dta, pres.dem.tercile != "Moderate" & big.state == 0))


stargazer(m, m1, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          keep = c("mismatch", "log.pop"),
          title = c("Effect of Partisan Mismatch on City Lobbying (Excluding California, 
                    Washington, and Texas)."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Partisan Mismatch", "Population (Log)"),
          add.lines = list(c("City FEs", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y"),
                           c("Full Controls", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
## Table A11: Effect of Partisan Mismatch on City Lobbying: Leads and Lags
###################################################################################################

m <- felm(lobby ~ mismatch + log.pop + log.income + log.own.source +
            pct.white + log.med.house | fips + state.by.year | 0 | fips, 
          data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m)

m1 <- felm(lobby ~ mismatch.lead1 + mismatch + log.pop + log.income + log.own.source +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m1)

m2 <- felm(lobby ~  mismatch.lead2 + mismatch.lead1 + mismatch + log.pop + log.income + 
             log.own.source + pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m2)

m3 <- felm(lobby ~  mismatch.lead2 + mismatch.lead1 + mismatch + mismatch.lag1 + log.pop + 
             log.income + log.own.source + pct.white + log.med.house | 
             fips + state.by.year | 0 | fips, 
           data = subset(dta, pres.dem.tercile != "Moderate"))
summary(m3)


## Table: Effect of Partisan Mismatch on City Lobbying: Leads and Lags
stargazer(m, m1, m2, m3, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05),
          title = c("Effect of Partisan Mismatch on City Lobbying: Leads and Lags."),
          keep = c("mismatch.lag1", "mismatch", "mismatch.lead1", "mismatch.lead2"),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Mismatch, t+2", "Mismatch, t+1", "Mismatch", "Mismatch, t-1"),
          add.lines = list(c("City FEs", "Y", "Y", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N), pretty(m2$N), pretty(m3$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1)), 
                             pretty(num.cities(m2)), pretty(num.cities(m3))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1), getmean(m2),  
                             getmean(m3))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



##################################################################################################
## Figure A2: Parallel trends
###################################################################################################

## Identify which cities never experienced a mismatch to create control group
tmp <- ddply(dta, .(fips), summarize,
             mismatch.cat = ifelse(sum(mismatch, na.rm = T) == 0, 
                                   "never.mismatch", "mismatch"))

table(tmp$mismatch.cat)

## Merge back in with main data
dta <- merge(dta, tmp, by = "fips")

## Control group: cities that never experienced a mismatch
control <- subset(dta, mismatch.cat == "never.mismatch")


## Get the average lobbying probability in each year for control cities 
lobby.prob <- ddply(control, .(year), summarize,
                    control.lobby.prob = mean(lobby, na.rm = T))

## Merge back with main data
dta1 <- merge(dta, lobby.prob, by = "year", all.x = T)

## Set levels on scaled.year variable. Year0 = year of mismatch for treated cities
dta1$scaled.year <- factor(dta1$scaled.year1, levels = c("year.lag2", "year.lag1", "year0"))
levels(dta1$scaled.year)

## Isolate to cities that experienced at least 2 years of no mismatch followed by a mismatch
sample <- subset(dta1, !is.na(scaled.year))


## Look at probability of lobbying by scaled year for treated cities. For control cities,
## use weighted average across years
prob.lobby <- ddply(sample, .(scaled.year), summarize,
                    treat.lobby = mean(lobby, na.rm = T),
                    control.lobby = mean(control.lobby.prob, na.rm = T))
prob.lobby

## Transform for plotting
prob.lobby1 <- melt(prob.lobby, id = "scaled.year")


## Add numeric values for scaled year variable for plotting
prob.lobby1$year <- rep(c(-2, -1, 0), 2)
levels(prob.lobby1[,2]) <- c("Mismatched Cities", "Never Mismatched Cities")
prob.lobby1


## Figure: Parallel Trends
## Plot average transfers for lobbying and non-lobbying cities by scaled year
(p <- ggplot(prob.lobby1, aes(year, value, linetype = variable)) +
    geom_point() + geom_line() +
    ylim(.35, .57) +
    scale_x_continuous(breaks = c(-2, -1, 0), limits = c(-2.25, .25)) +
    geom_vline(xintercept = -.5, linetype = 2) +
    theme_bw(base_family = "serif", base_size = 13) +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          strip.background = element_rect(fill = "white"),
          legend.position = c(.27, .65), legend.title = element_blank()) +
    labs(x = "Years Until Treatment", y = "Probability of Lobbying State") +
    annotate("text", x = -.1, y = .5, label = "Mismatch Occurs", family = "serif", size = 4))



###################################################################################################
## Table A12: Effect of Representative Ideology on City Lobbying
###################################################################################################

dta$np.quantile <- cut(dta$mean.np.lower, breaks = quantile(dta$mean.np.lower, 
                                                            probs = seq(0, 1, .2), na.rm = T),
                       include.lowest = TRUE, labels = c("Most Liberal", "Liberal", "Moderate", 
                                                         "Conservative", "Most Conservative"))


dta$mrp.tercile <- cut(dta$mrp.estimate, breaks = quantile(dta$mrp.estimate, 
                                                           probs = seq(0, 1, 1/3), na.rm = T),
                       include.lowest = TRUE, labels = c("Liberal", "Moderate", "Conservative"))


## Look at lobbying for liberal cities as representative ideology changes
m <- felm(lobby.lead1 ~ np.quantile + log.pop + log.own.source + log.income +
            pct.white + log.med.house | fips + state.by.year | 0 | fips, 
          data = subset(dta, mrp.tercile == "Liberal"))
summary(m)


m1 <- felm(lobby.lead1 ~ np.quantile + log.pop + log.own.source + log.income +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = subset(dta, mrp.tercile == "Conservative"))
summary(m1)


stargazer(m, m1, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05), 
          column.labels = c("Liberal Cities", "Conservative Cities"), keep = 1:4,
          title = c("Effect of Representative Ideology on City Lobbying."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Liberal Rep.", "Moderate Rep.", "Conservative Rep.",
                               "Most Conservative Rep."),
          add.lines = list(c("City FEs", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")



###################################################################################################
## Robustness Checks: RDD
###################################################################################################

## Load pacakges
library(rdd); library(rdrobust); library(rddtools); library(ggplot2)
library(AER); library(car); library(lmtest); library(plyr); library(rddensity)

## Load rdd.csv
rd <- read.csv("rdd.csv")



###################################################################################################
## Table A13
###################################################################################################

table(rd$mov1 < 2 & rd$mov1 > -2)
table(rd$mov1 < 5 & rd$mov1 > -5)
table(rd$mov1 < 10 & rd$mov1 > -10)
table(rd$mov1 < 20 & rd$mov1 > -20)

hist(rd$mov1)


###################################################################################################
## Table A14: RDD Results: Tercile Specification
###################################################################################################

## Re-specify mismatch based on tercile (rather than median)
rd$rep.mismatch <- ifelse((rd$party == "D" & (rd$pres.dem.tercile == "Republican" | 
                                                rd$mrp.tercile== "Conservative")) |
                            (rd$party == "R" & (rd$mrp.tercile == "Liberal" | 
                                                  rd$pres.dem.tercile == "Democrat")), 1, 0)

## Create running variable
rd$mov2 <- ifelse(rd$rep.mismatch == 1, rd$pct.mov, -rd$pct.mov)

summary(rdrobust(rd$lobby, rd$mov2, kernel = "triangular", p = 1, 
                 bwselect = "mserd", cluster = rd$fips))

summary(rdrobust(rd$lobby, rd$mov2, kernel = "triangular", p = 2, 
                 bwselect = "mserd", cluster = rd$fips))

summary(rdrobust(rd$lobby, rd$mov2, kernel = "triangular", p = 3, 
                 bwselect = "mserd", cluster = rd$fips))



summary(rdrobust(rd$lobby, rd$mov2, kernel = "triangular", p = 1, 
                 bwselect = "mserd", cluster = rd$fips, covs = cbind(rd$log.pop)))

summary(rdrobust(rd$lobby, rd$mov2, kernel = "triangular", p = 2,
                 bwselect = "mserd", cluster = rd$fips, covs = cbind(rd$log.pop)))

summary(rdrobust(rd$lobby, rd$mov2, kernel = "triangular", p = 3, bwselect = "mserd", 
                 cluster = rd$fips, covs = cbind(rd$log.pop)))



###################################################################################################
## Table A15: RDD Results: Lagged DV
###################################################################################################

summary(rdrobust(rd$lobby.lag1, rd$mov1, kernel = "triangular", p = 1, 
                 bwselect = "mserd", cluster = rd$fips))

summary(rdrobust(rd$lobby.lag1, rd$mov1, kernel = "triangular", p = 2, 
                 bwselect = "mserd", cluster = rd$fips))

summary(rdrobust(rd$lobby.lag1, rd$mov1, kernel = "triangular", p = 3, 
                 bwselect = "mserd", cluster = rd$fips))


summary(rdrobust(rd$lobby.lag1, rd$mov1, kernel = "triangular", p = 1, 
                 bwselect = "mserd", cluster = rd$fips, covs = cbind(rd$log.pop)))

summary(rdrobust(rd$lobby.lag1, rd$mov1, kernel = "triangular", p = 2, bwselect = "mserd", 
                 cluster = rd$fips, covs = cbind(rd$log.pop)))

summary(rdrobust(rd$lobby.lag1, rd$mov1, kernel = "triangular", p = 3, bwselect = "mserd", 
                 cluster = rd$fips, covs = cbind(rd$log.pop)))



###################################################################################################
## Figure A4: RDD Balance Checks
###################################################################################################

## Population
par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(2.5, 0.6, 0))
rdplot(rd$log.pop, rd$mov1, binselect = "es", x.lim = c(-40, 40), y.lim = c(10, 12), 
       nbins = c(15, 15), ci = 95, x.label = "", y.label = "", col.lines = "black", title = "")
title(cex.lab = 1.2, ylab = "Population (Log)", 
      xlab = "Margin of Victory for Unaligned Candidate")

## Income
par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(2.5, 0.6, 0))
rdplot(rd$log.income, rd$mov1, binselect = "es", x.lim = c(-40, 40), y.lim = c(10.5, 11.5), 
       nbins = c(15, 15), ci = 95, x.label = "", y.label = "", col.lines = "black", title = "")
title(cex.lab = 1.2, ylab = "Median Income (Log)", 
      xlab = "Margin of Victory for Unaligned Candidate")

## Percent White
par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(2.5, 0.6, 0))
rdplot(rd$pct.white, rd$mov1, binselect = "es", x.lim = c(-40, 40), y.lim = c(.5, .9), 
       nbins = c(15, 15), ci = 95, x.label = "", y.label = "", col.lines = "black", title = "")
title(cex.lab = 1.2, ylab = "Percent White", 
      xlab = "Margin of Victory for Unaligned Candidate")

## Median House Value
par(mar = c(5, 5, 2, 2), family = "serif", tck = -.018, las = 1, mgp = c(2.5, 0.6, 0))
rdplot(rd$log.med.house, rd$mov1, binselect = "es", x.lim = c(-40, 40), nbins = c(15, 15),
       ci = 95, x.label = "", y.label = "", col.lines = "black", title = "")
title(cex.lab = 1.2, ylab = "Median House Value (Log)", 
      xlab = "Margin of Victory for Unaligned Candidate")



###################################################################################################
## Additional Results
###################################################################################################


###################################################################################################
## Table A16: Effect of Individual Representative vs. Chamber Mismatch
###################################################################################################

dta <- read.csv("panel.csv")

## Specify chamber mismatch based on partisan proportion in lower house
dta$chamber.mismatch <- ifelse((dta$pres.dem.tercile == "Democrat" & dta$house.dem.prop < .5) |
                              (dta$pres.dem.tercile == "Republican" & dta$house.dem.prop > .5), 1, 0)
table(dta$chamber.mismatch)


## Specify governor mismatch based on party of governor
dta$gov.mismatch <- ifelse(dta$pres.dem.tercile == "Democrat" & dta$govparty == "R" |
                             dta$pres.dem.tercile == "Republican" & dta$govparty == "D", 1, 0)
table(dta$gov.mismatch)

## When more than 50% of other party control house: slight increase in lobbying
m <- felm(lobby ~ mismatch + chamber.mismatch + log.pop + log.own.source + log.income +
            pct.white + log.med.house | fips + state.by.year | 0 | fips, 
          data = subset(dta, dta$pres.dem.tercile != "Moderate"))
summary(m)

## No effect of governor mismatch
m1 <- felm(lobby ~ mismatch + gov.mismatch + log.pop + log.own.source + log.income +
             pct.white + log.med.house | fips + state.by.year | 0 | fips, 
           data = subset(dta, dta$pres.dem.tercile != "Moderate"))

stargazer(m, m1, omit.table.layout = "s", type = "text", star.cutoffs = c(0.05), keep = c(1:3),
          title = c("Effect of Individual Representative vs. Chamber Mismatch on City Lobbying."),
          dep.var.caption = "Probability of Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("District Mismatch", "Chamber Mismatch", "Governor Mismatch"),
          add.lines = list(c("City FEs", "Y", "Y"),
                           c("State-Year FEs", "Y", "Y"),
                           c("Full Controls", "Y", "Y", "Y", "Y"),
                           c("Observations", pretty(m$N), pretty(m1$N)),
                           c("\\# Cities", pretty(num.cities(m)), pretty(num.cities(m1))),
                           c("Mean Lobbying Probability", getmean(m), getmean(m1))),
          notes.append = FALSE, notes.align = "l", notes.label  = "",
          notes = "Robust standard errors clustered by city. $^{*}$p$<$0.05")

